
const util = require('../../../utils/util.js')
const domainName = getApp().globalData.domainName
const oss_domain = getApp().globalData.oss_domain
const parameter = getApp().globalData.parameter

const page_init=(that,jg_id)=>{
    wx.showLoading({
      title: '大量数据计算中……',
    })
    wx.request({
      url: domainName + '/testPaper_library_init',
      data: {
        jg_id,
      },
      ...parameter,
      success: res => {
        let date_list = res.data.date_list
        let show_list = that.data.show_list
        // 设置默认显示列表前两天
        if(date_list.length>1){
          show_list.push(date_list[0].start_date, date_list[1].start_date)
        }else if(date_list.length==1){
          show_list.push(date_list[0].start_date)
        }
        that.setData({
          date_list:date_list,
          show_list:show_list,
        })
        wx.hideLoading()
      },
    })
  }
const load_testPapers_by_label=(that, label_id)=>{
  wx.showLoading({
    title: '大量数据计算中……',
  })
  wx.request({
    url: domainName + '/load_testPapers_by_label',
    data: {
      label_id,
    },
    ...parameter,
    success: res => {
      // console.log('res',res.data)
      let date_list = res.data.date_list
      let show_list = that.data.show_list
      // 设置默认显示列表前两天
      if(date_list.length>1){
        show_list.push(date_list[0].start_date, date_list[1].start_date)
      }else if(date_list.length==1){
        show_list.push(date_list[0].start_date)
      }
      that.setData({
        date_list:date_list,
        show_list:show_list,
      })
      wx.hideLoading()
    },
  })
}
// 显示某日的试卷列表
const tap_dateItem=(that,e)=>{
  console.log('显示某日的试卷列表')
  var index1 = e.currentTarget.id
  var date_list = that.data.date_list
  var start_date = date_list[index1].start_date
  var show_list = that.data.show_list
  // 显示功能
  let date_index = show_list.indexOf(start_date)
  if(date_index>-1){ 
    show_list.splice(date_index,1)
    that.setData({
      show_list:show_list
    })
  }else{
    show_list.push(start_date)
    // 仅操作展开时，才加载数据
    wx.showLoading({
      title: '加载数据中……',
    })
    wx.request({
      url: domainName + '/load_testPapers_by_date',
      data: {
        jg_id:that.jg_id,
        date_set:start_date,
      },
      ...parameter,
      success: res => {
        date_list[index1].test_papers = res.data.test_paper_list
        that.setData({
          date_list:date_list,
          show_list:show_list
        })
        wx.hideLoading()
      },
    })
  }


}

const delete_testPaper=(that, e)=>{
  var date_list = that.data.date_list
  var index1 = e.currentTarget.dataset.index1
  var index2 = e.currentTarget.dataset.index2
  var test_paper_id = date_list[index1].test_papers[index2].test_paper_id
  wx.request({
    url: domainName + '/delete_testPaper',
    data: {
      test_paper_id,
    },
    ...parameter,
    success: res => {
      var delete_res = res.data.dalete_res
      if(delete_res==0){
        wx.showModal({
          content: '该试卷已有学员提交，故不允许删除',
          showCancel: false,
          title: '提醒',
        })
      }else if(delete_res){
        wx.showToast({
          title: '删除完成',
          duration: 1500,
          icon: 'success',
        })
        date_list[index1].test_papers.splice(index2,1)
        that.setData({
          date_list,
        })
      }
      wx.hideLoading()
    },
  })

}
// 设置试卷状态
const set_paperStatus=(that, e)=>{
  // console.log('设置试卷状态', e)
  let index1 = e.currentTarget.dataset.index1
  let index2 = e.currentTarget.dataset.index2
  let value = e.detail.value
  let date_list = that.data.date_list
  wx.request({
    url: domainName + '/set_paper_status',
    data: {
      test_paper_id:date_list[index1].test_papers[index2].test_paper_id,
      status:value && '1' || '2'
    },
    ...parameter,
    success: res => {
      // console.log('res',res.data)
      if(res.data.status){
        wx.showModal({
          content: res.data.decription,
          showCancel: false,
          title: '提示',
        })
      }
      date_list[index1].test_papers[index2].status = res.data.status
      that.setData({
        date_list:date_list,
      })
    },
    fail: com=>{
      that.setData({
        date_list:date_list,
      })
    }
  })
  
}
const show_labels=(that)=>{
  wx.request({
    url: domainName + '/load_jgLabels_for_paper',
    data: {
      jg_id:that.jg_id,
    },
    ...parameter,
    success: res => {
      that.setData({
        label_list:res.data.label_list,
        show:true,
      })
    },
  })
}
// 页面数据重置
const rest_page=(that)=>{
  that.setData({
    show:false,
    show_list:[],
    label_name:'分组/批次',
    show_label_index:null,
    confirm_label_index:null,
  })
}
// 选择标签
const select_label = (that,e) => {
  that.setData({
    show_label_index:e.currentTarget.id,
  })
} 
// 清除已选标签
const clear = (that) => {
  // 重置页面
  rest_page(that)
  page_init(that, that.jg_id)
}
// 确定选择
const confirm_label = (that) => {
  that.setData({
    label_name:that.data.label_list[that.data.show_label_index].label_name,
    confirm_label_index:that.data.show_label_index,
    show:false,
    show_list:[],
  })
  let selected_label_id = that.data.label_list[that.data.show_label_index].label_id
  load_testPapers_by_label(that,selected_label_id)
}
// 查看学员完成情况
const tap_finished_count=(that,e)=>{
  let index1 = e.currentTarget.dataset.index1
  let index2 = e.currentTarget.dataset.index2
  let test_paper_id = that.data.date_list[index1].test_papers[index2].test_paper_id
  let test_name = that.data.date_list[index1].test_papers[index2].test_name
  wx.navigateTo({
    url:'/pages/doTi/finished_count/finished_count?test_paper_id=' + test_paper_id + '&test_paper_name=' + test_name,
  })
}
// 查看试卷详情
const tap_paper=(that,e)=>{
  let index1 = e.currentTarget.dataset.index1
  let index2 = e.currentTarget.dataset.index2
  let test_paper_id = that.data.date_list[index1].test_papers[index2].test_paper_id
  wx.navigateTo({
    url:'/pages/doTi/paper_details/paper_details?test_paper_id=' + test_paper_id,
  })
}
// 发布新试卷
const add_testPaper=(that, e)=>{
  wx.navigateTo({
    url: '/pages/doTi/add_paper/add_paper?jg_id=' + that.jg_id
  })
}
// 前往批次/分组管理页面
const mana_label=(that)=>{
  wx.navigateTo({
    url: '/pages/jgs/jg_labels/jg_labels?jg_id=' + that.jg_id
  })
  that.setData({
    show:false
  })
  
}

// 长按试卷名称
const longpress_paper=(that,e)=>{
  wx.showActionSheet({
    itemList: ['彻底删除'],
    success:res => {
      if(res.tapIndex==0){
        delete_testPaper(that,e)
      }
    },
  })
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    icon_url:oss_domain + '/static/picture/app_icon/',
    show:false,
    show_list:[],
    now:util.formatTime(new Date()),
    yestoday:util.daysLaterOrAfter(new Date(),-1),
    today:util.formatDate(new Date()),
    tomorrow:util.daysLaterOrAfter(new Date(),1),
    label_name:'分组/批次',
  },
 
  // 标签栏
  show_labels(e){getApp().get_vibration(); show_labels(this)},
  select_label(e){getApp().get_vibration(); select_label(this, e)},
  clear(e){getApp().get_vibration(); clear(this, e)},
  confirm_label(e){getApp().get_vibration(); confirm_label(this, e)},
  
  // 试卷
  set_paper_status(e){getApp().get_vibration(); set_paperStatus(this,e)},
  tap_paper(e){getApp().get_vibration(); tap_paper(this,e)},
  tap_finished_count(e){getApp().get_vibration(); tap_finished_count(this,e)},
  
  add_testPaper(e){getApp().get_vibration(); add_testPaper(this,e)},
  
  tap_dateItem(e){getApp().get_vibration(); tap_dateItem(this,e)},

  mana_label(e){getApp().get_vibration(); mana_label(this)},
  longpress_paper(e){getApp().get_vibration(); longpress_paper(this, e)},
  


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.jg_id = options.jg_id
    page_init(this, this.jg_id)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.setData({
      windowHeight : wx.getSystemInfoSync().windowHeight
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if(this.can_onShow){
      this.clear(this)
      this.can_onShow = false // 用于下一个页面反馈是否需要更新列表
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {
    console.log('用户点击右上角分享',e)
    let index1 = e.target.dataset.index1
    let index2 = e.target.dataset.index2

    getApp().get_vibration()
    let test_paper_id = this.data.date_list[index1].test_papers[index2].test_paper_id
    let test_name = this.data.date_list[index1].test_papers[index2].test_name

    return  {
      title: '【试卷】' + test_name,
      // imageUrl:'https://yqwl.top/app_images/caac.png',
      path: '/pages/doTi/paper_details/paper_details?test_paper_id=' + test_paper_id
    }
  }
})